package com.ruoyi.web.controller.dsrw;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TOrders;
import com.ruoyi.system.domain.TTakeoutLog;
import com.ruoyi.system.service.ITOrdersService;
import com.ruoyi.system.service.ITProductReviewsService;
import com.ruoyi.system.service.ITTakeoutLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ScheduledTasks {

    @Autowired
    private ITOrdersService tOrdersService;
    @Autowired
    private ITTakeoutLogService takeoutLogService;
    @Autowired
    private ITProductReviewsService tProductReviewsService;

    // 30分钟自动取消未支付订单
    @Scheduled(fixedRate = 60000)    //每1分钟执行一次
    public void taskWithFixedRate() {

        List<TOrders> list =  tOrdersService.getOneDay();
        for (int i = 0; i < list.size(); i++) {
            TOrders orders = list.get(i);
            // 1 取消订单
            orders.setId(orders.getId());
            orders.setOrderStatus(6L); // 订单状态（6表示"已取消"）
            orders.setCancelTime(new Date()); // 取消时间
            orders.setUpdateTime(new Date()); // 更新时间
            tOrdersService.updateTOrders(orders);
            // 2加订单跟踪
            TTakeoutLog tTakeoutLog = new TTakeoutLog();
            tTakeoutLog.setUserId(orders.getUserId());
            tTakeoutLog.setOrderId(orders.getId());
            tTakeoutLog.setNickname(orders.getReceiverName());
            tTakeoutLog.setLogTime(new Date());
            tTakeoutLog.setDescription("订单超时取消");
            tTakeoutLog.setOrderId(orders.getId());
            takeoutLogService.insertTTakeoutLog(tTakeoutLog);
        }

    }

    /**
     * 订单完成后超过5天提交评价
     */
//    @Scheduled(fixedRate = 60000)    //每1分钟执行一次
//    public void taskWithFixedRate() {
//        System.out.println("***************************************");
//        System.out.println(new Date());
//        System.out.println("每60秒执行一次.");
//
//        List<TOrders> list =  tOrdersService.getOneDay();
//        for (int i = 0; i < list.size(); i++) {
//            TOrders orders = list.get(i);
//            // 1 取消订单
//            orders.setId(orders.getId());
//            orders.setOrderStatus(6L); // 订单状态（6表示"已取消"）
//            orders.setCancelTime(new Date()); // 取消时间
//            orders.setUpdateTime(new Date()); // 更新时间
//            tOrdersService.updateTOrders(orders);
//            // 2加订单跟踪
//            TTakeoutLog tTakeoutLog = new TTakeoutLog();
//            tTakeoutLog.setUserId(orders.getUserId());
//            tTakeoutLog.setOrderId(orders.getId());
//            tTakeoutLog.setNickname(orders.getReceiverName());
//            tTakeoutLog.setLogTime(new Date());
//            tTakeoutLog.setDescription("订单超时取消");
//            tTakeoutLog.setOrderId(orders.getId());
//            takeoutLogService.insertTTakeoutLog(tTakeoutLog);
//        }
//
//    }

}
